package com.wsx.sky.http.config;

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author wangshuaixin
 * @Title: com.wsx.sky.http.config.RedisConfig
 * @Description: TODO
 * @date 2019/04/17
 */
@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    @Setter
    @Getter
    private String host;
    @Value("${spring.redis.port}")
    @Setter @Getter private int port;
    @Value("${spring.redis.database:0}")
    @Setter @Getter private int db;
    @Value("${spring.redis.password}")
    @Setter @Getter private String password;
    @Value("${spring.redis.timeout}")
    @Setter @Getter private int timeout;


    @Bean(name = "jedisPool")
    public JedisPool JedisPoolFactory() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(50);
        config.setMaxIdle(5);
        config.setMaxWaitMillis(2 * 1000);
        config.setTestOnBorrow(true);
        config.setTestOnReturn(false);
        JedisPool pool = null;
        if (StringUtils.isEmpty(getPassword())) {
            pool = new JedisPool(config, getHost(), getPort(), getTimeout(), null, getDb());
        } else {
            pool = new JedisPool(config, getHost(), getPort(), getTimeout(), getPassword(), getDb());
        }
        return pool;
    }
}
